package main

import (
	"fmt"
	"os"
	"sync"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

type FileWriter struct {
}

func (fw FileWriter) Levels() []logrus.Level {
	return logrus.AllLevels
}

func (fw FileWriter) Fire(entry *logrus.Entry) error {
	level := entry.Level
	// 获得等级
	filePath := fmt.Sprintf("./logs/%s.log", level)
	file, _ := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	line, _ := entry.String()
	file.Write([]byte(line))
	defer file.Close()
	return nil
}
func test() {
	for i := 0; i < 10; i++ {
		logrus.Warn("okkkk")
		logrus.Error("okkkk")
		logrus.Info("okkkk")
		logrus.Debug("okkkk")
	}
	wg.Done()
}

var wg sync.WaitGroup

func main() {
	router := gin.New()
	logrus.AddHook(&FileWriter{})
	logrus.SetLevel(logrus.DebugLevel)
	logrus.SetFormatter(MyFomatter{Prefix: "GinApp"})
	wg.Add(1)
	go test()
	router.Run("127.0.0.1:8003")
	wg.Wait()
}
